#include "mysettings.h"
#include "common.h"
#include "filelogger.h"
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QSettings>
#include <QDesktopWidget>
#include <QApplication>

// Costants
// _LIT(KBestScreenSnap, "screensnap");
const TUid KBestScreenSnap = { 0x2000a582 };
//const TUid KBestScreenSnap = { 0xe373a8b2 };
// _LIT(KBestTimer, "timer");
const TUid KBestTimer = { 0x20012550 };
// _LIT(KPowerBoot, "powerboot");
const TUid KPowerBoot = { 0x20008a14 };
// _LIT(KRamBlow, "ramblow");
const TUid KRamBlow = { 0xa000a5c1 };
// _LIT(KPhotoViewer, "photoviewer");
const TUid KPhotoViewer = { 0x2000737e };

void MySettings::SetBackgroundImage()
	{
	// Using QPalette you can set background image as follows.
	QPalette p = palette();

	// Load image to QPixmap, Give full path of image
	QString image = KPorImage;
	QPixmap pixmap1(image); // For emulator C: is ..\epoc32\winscw\c so image must be at that location
/*
	// resize image if it is larger than screen size.
	QDesktopWidget* desktopWidget = QApplication::desktop();
	QRect rect = desktopWidget->availableGeometry();

	QSize size(rect.width(), rect.height());
	// resize as per your requirement..
	QPixmap pixmap(pixmap1.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
*/
	p.setBrush(QPalette::Background, pixmap1);
	setPalette(p);
	}

void MySettings::ReadSettings()
	{
	QString settingsfile = KSettingsFile;
	QString group = KAuthor;
	QSettings settings(settingsfile, QSettings::IniFormat);
    settings.beginGroup(group);
 
    // Check if buttons are checked
    profilesettings->setChecked(settings.value("profilesettings").toBool());
    alarmsounds->setChecked(settings.value("alarmsounds").toBool());
    browserbookmark->setChecked(settings.value("browserbookmark").toBool());
    calendar->setChecked(settings.value("calendar").toBool());
    appshelldata->setChecked(settings.value("appshelldata").toBool());
    accesspoint->setChecked(settings.value("accesspoint").toBool());
    cookies->setChecked(settings.value("cookies").toBool());
    t9dictionary->setChecked(settings.value("t9dictionary").toBool());
    contacts->setChecked(settings.value("contacts").toBool());
    contactsindex->setChecked(settings.value("contactsindex").toBool());
    notes->setChecked(settings.value("notes").toBool());
    btregistry->setChecked(settings.value("btregistry").toBool());
    speeddial->setChecked(settings.value("speeddial").toBool());
    alarmserver->setChecked(settings.value("alarmserver").toBool());
    shortcutsbar->setChecked(settings.value("shortcutsbar").toBool());
    calllog->setChecked(settings.value("calllog").toBool());
    positions->setChecked(settings.value("positions").toBool());
    bestscreensnap->setChecked(settings.value("bestscreensnap").toBool());
    besttimer->setChecked(settings.value("besttimer").toBool());
    powerboot->setChecked(settings.value("powerboot").toBool());
    ramblow->setChecked(settings.value("ramblow").toBool());
    photoviewer->setChecked(settings.value("photoviewer").toBool());
    // Set at last position
    sms->setChecked(settings.value("sms").toBool());

    settings.endGroup();
	}

void MySettings::OnlyClose()
	{
    close();
    iParent->activateWindow(); // For non-touch
	}

void MySettings::SaveSettings()
	{
	QString settingsfile = KSettingsFile;
	QString group = KAuthor;
	QSettings settings(settingsfile, QSettings::IniFormat);
    settings.beginGroup(group);

    // Set checked status
    settings.setValue("profilesettings", profilesettings->isChecked());
    settings.setValue("alarmsounds", alarmsounds->isChecked());
    settings.setValue("browserbookmark", browserbookmark->isChecked());
    settings.setValue("calendar", calendar->isChecked());
    settings.setValue("appshelldata", appshelldata->isChecked());
    settings.setValue("accesspoint", accesspoint->isChecked());
    settings.setValue("cookies", cookies->isChecked());
    settings.setValue("t9dictionary", t9dictionary->isChecked());
    settings.setValue("contacts", contacts->isChecked());
    settings.setValue("contactsindex", contactsindex->isChecked());
    settings.setValue("notes", notes->isChecked());
    settings.setValue("btregistry", btregistry->isChecked());
    settings.setValue("speeddial", speeddial->isChecked());
    settings.setValue("alarmserver", alarmserver->isChecked());
    settings.setValue("shortcutsbar", shortcutsbar->isChecked());
    settings.setValue("calllog", calllog->isChecked());
    settings.setValue("positions", positions->isChecked());
    settings.setValue("bestscreensnap", bestscreensnap->isChecked());
    settings.setValue("besttimer", besttimer->isChecked());
    settings.setValue("powerboot", powerboot->isChecked());
    settings.setValue("ramblow", ramblow->isChecked());
    settings.setValue("photoviewer", photoviewer->isChecked());
    // Set at last position
    settings.setValue("sms", sms->isChecked());

	settings.endGroup();
	}

void MySettings::check()
{
	ReadSettings();
}

void MySettings::save()
{
    QString tempfile = KMainFile;
    QString tempstring = "";
    QString DisableText = KDisableText;
    __LOGSTR_TOFILE("Save Settings...");
    
    // check what to save
    if (profilesettings->isChecked())
        tempstring.append("C:\\private\\10202be9\\persists\\101f8798.cre > E:\\Backup\\101f8798.cre\n");
    if (alarmsounds->isChecked())
        tempstring.append("C:\\private\\10202be9\\persists\\101f874d.cre > E:\\Backup\\101f874d.cre\n");
    if (browserbookmark->isChecked())
        tempstring.append("C:\\private\\100012a5\\DBS_101FD685_BrowserBookmarks.db > E:\\Backup\\DBS_101FD685_BrowserBookmarks.db\n");
    if (calendar->isChecked())
        tempstring.append("C:\\private\\10003a5b\\Calendar > E:\\Backup\\Calendar\n");
    if (appshelldata->isChecked())
        tempstring.append("C:\\private\\101f4cd2\\content\\appshelldata.xml > E:\\Backup\\appshelldata.xml\n");
    if (accesspoint->isChecked())
        tempstring.append("C:\\private\\10202be9\\persists\\cccccc00.cre > E:\\Backup\\cccccc00.cre\n");
    if (cookies->isChecked())
        tempstring.append("C:\\Private\\101f8530\\cookies.dat > E:\\Backup\\cookies.dat\n");
    if (t9dictionary->isChecked())
        tempstring.append("C:\\predic\\101F8615_F.dat > E:\\Backup\\101F8615_F.dat\n");
    if (contacts->isChecked())
        tempstring.append("C:\\private\\10003a73\\contacts.cdb > E:\\Backup\\contacts.cdb\n"
                          "C:\\private\\100012a5\\DBS_100065FF_Contacts.cdb > E:\\Backup\\DBS_100065FF_Contacts.cdb\n");
    if (contactsindex->isChecked())
        tempstring.append("C:\\private\\100012a5\\DBS_20019536_FrenFriendDb.db > E:\\Backup\\DBS_20019536_FrenFriendDb.db\n");
    if (notes->isChecked())
        tempstring.append("C:\\private\\100012a5\\DBS_101F8878_Notepad.dat > E:\\Backup\\DBS_101F8878_Notepad.dat\n");
    if (btregistry->isChecked())
        tempstring.append("C:\\private\\100012a5\\DBS_100069CC_btregistry > E:\\Backup\\DBS_100069CC_btregistry\n");
    if (speeddial->isChecked())
        tempstring.append("C:\\private\\10003a73\\Cntmodel.ini > E:\\Backup\\Cntmodel.ini\n");
    if (alarmserver->isChecked())
        tempstring.append("C:\\private\\101f5027\\AlarmServer.ini > E:\\Backup\\AlarmServer.ini\n");
    if (shortcutsbar->isChecked())
        tempstring.append("C:\\private\\10202be9\\persists\\10275104.cre > E:\\Backup\\10275104.cre\n");
    if (calllog->isChecked())
        tempstring.append("C:\\private\\101f401d\\Logdbu.dat > E:\\Backup\\Logdbu.dat\n");
    if (positions->isChecked())
        tempstring.append("C:\\private\\100012a5\\DBS_101FE978_eposlm.ldb > E:\\Backup\\DBS_101FE978_eposlm.ldb\n");
    if (bestscreensnap->isChecked())
    	{
		TPtrC thisdrive = ParentClass.GetAppDrive(KBestScreenSnap);
		QString tempscreen((QChar*)thisdrive.Ptr(),thisdrive.Length());
		tempstring.append(tempscreen);
        tempstring.append("\\private\\2000a582 > E:\\Backup\\2000a582\n");
    	}
    if (besttimer->isChecked())
    	{
		TPtrC thisdrive = ParentClass.GetAppDrive(KBestTimer);
		QString temptimer((QChar*)thisdrive.Ptr(),thisdrive.Length());
		tempstring.append(temptimer);
        tempstring.append("\\private\\20012550 > E:\\Backup\\20012550\n");
    	}
    if (powerboot->isChecked())
    	{
		TPtrC thisdrive = ParentClass.GetAppDrive(KPowerBoot);
		QString temppower((QChar*)thisdrive.Ptr(),thisdrive.Length());
		tempstring.append(temppower);
        tempstring.append("\\private\\20008a14 > E:\\Backup\\20008a14\n");
    	}
    if (ramblow->isChecked())
    	{
		TPtrC thisdrive = ParentClass.GetAppDrive(KRamBlow);
		QString tempram((QChar*)thisdrive.Ptr(),thisdrive.Length());
		tempstring.append(tempram);
        tempstring.append("\\private\\a000a5c1 > E:\\Backup\\a000a5c1\n");
    	}
    if (photoviewer->isChecked())
    	{
		TPtrC thisdrive = ParentClass.GetAppDrive(KPhotoViewer);
		QString tempphoto((QChar*)thisdrive.Ptr(),thisdrive.Length());
		tempstring.append(tempphoto);
        tempstring.append("\\private\\2000737e > E:\\Backup\\2000737e\n");
    	}
    // Set at last position
    if (sms->isChecked())
        tempstring.append("C:\\private\\1000484b\\Mail2 > E:\\Backup\\SMS\n");

    tempstring.append(DisableText);
    AppendOnFileL(tempfile, tempstring);
    SaveSettings();
    OnlyClose();
}

void MySettings::AppendOnFileL(QString filename, QString text)
{
    QFile file(filename);
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&file);
    out << text;
    file.close();
}

void MySettings::checkAll()
{
    /* Fuction to set checked */
    profilesettings->setChecked(true);
    alarmsounds->setChecked(true);
    browserbookmark->setChecked(true);
    calendar->setChecked(true);
    appshelldata->setChecked(true);
    accesspoint->setChecked(true);
    cookies->setChecked(true);
    t9dictionary->setChecked(true);
    contacts->setChecked(true);
    contactsindex->setChecked(true);
    notes->setChecked(true);
    btregistry->setChecked(true);
    speeddial->setChecked(true);
    alarmserver->setChecked(true);
    shortcutsbar->setChecked(true);
    calllog->setChecked(true);
    positions->setChecked(true);
    bestscreensnap->setChecked(true);
    besttimer->setChecked(true);
    powerboot->setChecked(true);
    ramblow->setChecked(true);
    photoviewer->setChecked(true);
    // Set at last position
    sms->setChecked(true);
}

void MySettings::uncheckAll()
{
    /* Fuction to unset checked */
    profilesettings->setChecked(false);
    alarmsounds->setChecked(false);
    browserbookmark->setChecked(false);
    calendar->setChecked(false);
    appshelldata->setChecked(false);
    accesspoint->setChecked(false);
    cookies->setChecked(false);
    t9dictionary->setChecked(false);
    contacts->setChecked(false);
    contactsindex->setChecked(false);
    notes->setChecked(false);
    btregistry->setChecked(false);
    speeddial->setChecked(false);
    alarmserver->setChecked(false);
    shortcutsbar->setChecked(false);
    calllog->setChecked(false);
    positions->setChecked(false);
    bestscreensnap->setChecked(false);
    besttimer->setChecked(false);
    powerboot->setChecked(false);
    ramblow->setChecked(false);
    photoviewer->setChecked(false);
    // Set at last position
    sms->setChecked(false);
}

MySettings::MySettings(QWidget *parent) :
    QDialog(parent)
{
	iParent = parent;
	SetBackgroundImage(); // Set Image
	
	/* Create Settings Label Title */
	label = new QLabel(tr("Settings"));
	// label->setStyleSheet(tr("QLabel {font-weight: bold;}"));
	label->setStyleSheet(ParentClass.GetButtonSS());
	
    /* Create a GroupBox */
    groupBox = new QGroupBox(tr(""));

    /* Create all checkbox */
    profilesettings = new QCheckBox(tr("Profile Settings"));
    alarmsounds = new QCheckBox(tr("Alarm Sounds"));
    browserbookmark = new QCheckBox(tr("Browser Bookmarks"));
    calendar = new QCheckBox(tr("Calendar"));
    appshelldata = new QCheckBox(tr("Icons Position"));
    accesspoint = new QCheckBox(tr("Access Points"));
    cookies = new QCheckBox(tr("Cookies"));
    t9dictionary = new QCheckBox(tr("T9 Dictionary"));
    contacts = new QCheckBox(tr("Contacts"));
    contactsindex = new QCheckBox(tr("Contacts Index"));
    notes = new QCheckBox(tr("Notes"));
    btregistry = new QCheckBox(tr("Bluetooth Registry"));
    speeddial = new QCheckBox(tr("Speed Dial"));
    alarmserver = new QCheckBox(tr("Alarms"));
    shortcutsbar = new QCheckBox(tr("Shortcuts Bar"));
    calllog = new QCheckBox(tr("Call Log"));
    positions = new QCheckBox(tr("GPS Positions"));
    bestscreensnap = new QCheckBox(tr("Best ScreenSnap"));
    besttimer = new QCheckBox(tr("Best Timer"));
    powerboot = new QCheckBox(tr("PowerBoot"));
    ramblow = new QCheckBox(tr("RAM Blow"));
    photoviewer = new QCheckBox(tr("Resco PhotoViewer"));

    // Set at last position
    sms = new QCheckBox(tr("SMS Folder"));
    
    /* Function that controls which elements are enabled */
    check();

    /* Create a Vertical Layout for checkbox */
    vbox = new QVBoxLayout;
    vbox->addWidget(sms);
    vbox->addWidget(profilesettings);
    vbox->addWidget(alarmsounds);
    vbox->addWidget(browserbookmark);
    vbox->addWidget(calendar);
    vbox->addWidget(appshelldata);
    vbox->addWidget(accesspoint);
    vbox->addWidget(cookies);
    vbox->addWidget(t9dictionary);
    vbox->addWidget(contacts);
    vbox->addWidget(contactsindex);
    vbox->addWidget(notes);
    vbox->addWidget(btregistry);
    vbox->addWidget(speeddial);
    vbox->addWidget(alarmserver);
    vbox->addWidget(shortcutsbar);
    vbox->addWidget(calllog);
    vbox->addWidget(positions);
    vbox->addWidget(bestscreensnap);
    vbox->addWidget(besttimer);
    vbox->addWidget(powerboot);
    vbox->addWidget(ramblow);
    vbox->addWidget(photoviewer);
    //vbox->addStretch(1);

    /* Set GroupBox Layout */
    groupBox->setLayout(vbox);

    /* Create a Scrollbar */
    scroll = new QScrollArea(this);
    scroll->setWidget(groupBox);
    scroll->setWidgetResizable(true);

    /* CheckAll Button */
    checkallButton = new QPushButton(this);
    connect(checkallButton, SIGNAL(clicked()), this, SLOT(checkAll()));
    checkallButton->setText("Check All");
    checkallButton->setStyleSheet(ParentClass.GetButtonSS());

    /* UncheckAll Button */
    uncheckallButton = new QPushButton(this);
    connect(uncheckallButton, SIGNAL(clicked()), this, SLOT(uncheckAll()));
    uncheckallButton->setText("Uncheck All");
    uncheckallButton->setStyleSheet(ParentClass.GetButtonSS());

    /* Save Button */
    okButton = new QPushButton(this);
    connect(okButton, SIGNAL(clicked()), this, SLOT(save()));
    okButton->setText("Save");
    okButton->setStyleSheet(ParentClass.GetButtonSS());

    /* Back Button */
    backButton = new QPushButton(this);
    connect(backButton, SIGNAL(clicked()), this, SLOT(OnlyClose()));
    backButton->setText("Back");
    backButton->setStyleSheet(ParentClass.GetButtonSS());

    /* Right SoftKey */
    backButtonMenu = new QAction(tr("Back"), this);
    connect( backButtonMenu, SIGNAL(triggered()), this, SLOT( close() ) );
    backButtonMenu->setSoftKeyRole( QAction::NegativeSoftKey );
    addAction( backButtonMenu );

    /* Stylize all in a Grid */
    layout = new QGridLayout;
    layout->addWidget(label, 0, 0, 1, 2, Qt::AlignHCenter | Qt::AlignTop);
    layout->addWidget(scroll, 1, 0, 1, 2);
    layout->addWidget(checkallButton, 2, 0, 1, 1);
    layout->addWidget(uncheckallButton, 2, 1, 1, 1);
    layout->addWidget(okButton, 3, 0, 1, 1);
    layout->addWidget(backButton, 3, 1, 1, 1);
    setLayout(layout);
}
